package com.tkjt.pms.sso;

import java.util.Map;
import java.util.Set;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.m3rd.Sso3rd;
import org.jeecg.modules.m3rd.common.Sso3rdConsts;
import org.jeecg.modules.m3rd.common.Sso3rdUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.lang.UUID;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("ssoTkPms")
public class Sso3rdTkService implements Sso3rd {

	@Override
	public Result<String> doSso3rd(String username, String password, Map<String, String> sssParams) {

		String ssoPrefix = "ssoTkPms";
		Result<String> result = new Result<String>();
		try {
			result = this.innerSso3rdProcessor(ssoPrefix, username, password, sssParams);
			return result;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		result.setResult(Sso3rdConsts.SSO3RD_RET_FAIL);
		return result;
	}

	private Result<String> innerSso3rdProcessor(String ssoPrefix, String username, String password, Map<String, String> sssParams) {

		Result<String> result = new Result<String>();

		// Url
		String tkssoUrl = sssParams.get(Sso3rdUtils.getUrlKey(ssoPrefix));
		log.info("string tkssoUrl={}", tkssoUrl);

		// Headers
		HttpHeaders headers = new HttpHeaders();
		String valueKey = Sso3rdUtils.getHeadersKey(ssoPrefix);
		JSONObject jsonObj = null;
		String strHeaders = sssParams.get(valueKey);
		log.info("string tkssoHeaders={}\r\n", strHeaders);
		if (null != strHeaders) {
			jsonObj = (JSONObject) JSONObject.parse(strHeaders);
			log.info("jsonObj tkssoHeaders={}\r\n", jsonObj);
			if (!jsonObj.containsKey("TK_BUSINESS_SERIALID") || jsonObj.getString("TK_BUSINESS_SERIALID").isEmpty()) {
				jsonObj.put("TK_BUSINESS_SERIALID", UUID.randomUUID().toString());
			}
			Set<String> keys = jsonObj.keySet();
			for (String key : keys) {
				headers.add(key, jsonObj.getString(key));
			}
		}
		log.info("http tkssoHeaders={}\r\n", headers);

		// Variables
		JSONObject variables = null;
		valueKey = Sso3rdUtils.getVariablesKey(ssoPrefix);
		String tkssoVariables = sssParams.get(valueKey);
		log.info("string variables={}\r\n", tkssoVariables);
		if (null != tkssoVariables) {
			variables = (JSONObject) JSONObject.parse(tkssoVariables);
		}
		log.info("jsonObj variables={}\r\n", variables);

		// Body
		valueKey = Sso3rdUtils.getBodyKey(ssoPrefix);
		JSONObject params = null;
		String tkssoParams = sssParams.get(valueKey);
		log.info("string params={}\r\n", tkssoParams);
		if (null == tkssoParams) {
			tkssoParams = "{}";
		}
		params = (JSONObject) JSONObject.parse(tkssoParams);
		log.info("jsonObj params={}\r\n", params);
		if (!params.containsKey("loginName")) {
			params.put("loginName", username);
		}
		if (!params.containsKey("password")) {
			params.put("password", password);
		}
		if (!params.containsKey("typeCd")) {
			params.put("typeCd", "5");
		}
		log.info("jsonObj body={}\r\n", params);

		// DO POST
		try {
			ResponseEntity<JSONObject> response = RestUtil.request(tkssoUrl, HttpMethod.POST, headers, variables, params, JSONObject.class);
			log.info("response status code:{}", response.getStatusCode());
			log.info("response body:\r\n{}", response.getBody());
			log.info("response headers:\r\n{}", response.getHeaders());
			String code = (String)response.getBody().get("code");
			log.info("code={}", code);
			if (null != code && code.equals("0")) {
				result.setResult(Sso3rdConsts.SSO3RD_RET_SUCCESS);
				log.info("result={}", result);
				return result;
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		result.setResult(Sso3rdConsts.SSO3RD_RET_FAIL);
		return result;

	}

}
